package com.quan.system.service.impl;

import com.quan.system.entity.SystemArea;
import com.quan.system.mapper.SystemAreaMapper;
import com.quan.system.service.SystemAreaService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quan.common.core.MyBaseServiceImpl;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 地区表 服务实现类
 * </p>
 *
 * @author 杨浩泉
 * @since 2019-06-03
 */
@Service
public class SystemAreaServiceImpl extends MyBaseServiceImpl<SystemAreaMapper, SystemArea> implements SystemAreaService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Async
	@Override
	public boolean asyncUpdateLocation(SystemArea entity) {
		return super.updateById(entity);
	}

	@Async
	@Override
	public void asyncGeneratorVantAreaJson() {
		
		Map<String, Object> province = new HashMap<String, Object>() ;
		Map<String, Object> city = new HashMap<String, Object>() ;
		Map<String, Object> county = new HashMap<String, Object>() ;
		
		
		QueryWrapper<SystemArea> queryWrapper1 = new QueryWrapper<SystemArea>();
		queryWrapper1.eq("level", 1);
		List<SystemArea> provinces = super.list(queryWrapper1) ;
		for (SystemArea area : provinces) {
			province.put(String.valueOf(area.getId()), area.getName()) ;
		}
		
		QueryWrapper<SystemArea> queryWrapper2 = new QueryWrapper<SystemArea>();
		queryWrapper2.eq("level", 2);
		List<SystemArea> citys = super.list(queryWrapper2) ;
		for (SystemArea area : citys) {
			city.put(String.valueOf(area.getId()), area.getName()) ;
		}
		
		QueryWrapper<SystemArea> queryWrapper3 = new QueryWrapper<SystemArea>();
		queryWrapper3.eq("level", 2);
		List<SystemArea> countys = super.list(queryWrapper3) ;
		for (SystemArea area : countys) {
			county.put(String.valueOf(area.getId()), area.getName()) ;
		}
		
		Map<String, Object> exportdefault = new HashMap<String, Object>() ;
		exportdefault.put("province_list", province);
		exportdefault.put("city_list", city);
		exportdefault.put("county_list", county);
		
		try {
			String path = "d:/" ;
			InputStream inputStream = IOUtils.toInputStream(JSON.toJSONString(exportdefault), "UTF-8") ;
			IOUtils.copy(inputStream, new FileOutputStream(path + "Area.json"));
			logger.debug("输出路径：" + path);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}
